<?php
/**
 * Created by IntelliJ IDEA.
 * User: fang.cai.liang@aliyun.com
 * Date: 2017/7/3
 * Time: 15:26
 */

namespace nflow;

/**
 * 粤亮信息科技有限公司 API
 * Class UmeolOperator
 * @package nflow
 */
class UmeolOperator extends AOperator
{

    /**
     * @var array
     */
    private $config = [];

    /**
     * UmeolOperator constructor.
     * @param $config
     */
    function __construct($config){
        $this->setConfig($config);
    }

    /**
     * @return array
     */
    public function getConfig(){
        return $this->config;
    }

    /**
     * @param $config
     */
    public function setConfig($config){
        if($config && is_array($config) && !empty($config)){
            $this->config = $config;
        }
    }

    private $commonMap = [
        -100 => 1000,
        -101 => 1001,
        -105 => 1002,
        -112 => 1003,
        -113 => 1004,
        -2 => 1005,
        -7 => 1002,
        -21 => 1006,
        -22 => 1007,
        -23 => 1006,
        2 => 1002
    ];

    private $operatorMap = [
        1 => OPERATOR_ID_TELECOM,
        3 => OPERATOR_ID_UNICOM,
        5 => OPERATOR_ID_MOBILE,
        4 => OPERATOR_ID_ALL
    ];

    /**
     * 生成 key
     * @param $phone
     * @param $timestamp
     * @return string
     */
    private function genUserKey($phone, $timestamp){
        $did = $this->config['did'];
        $secret = $this->config['secret'];
        return md5($phone.$did.$timestamp.$secret);
    }

    /**
     * 生成 发送数据
     * @param $phone 电话号码
     * @param $timestamp 时间戳
     * @param $userkey 生成的 token
     * @param null $efftype 下月生效还是立即生效
     * @param null $dorderid 
     * @param null $bid 产品 ID
     * @return array
     */
    private function buildData($phone, $timestamp, $userkey, $efftype = null, $dorderid = null, $bid = null){
        $data = [
            'tel' => $phone,
            'did' => $this->config['did'],
            'timestamp' => $timestamp,
            'userkey' => $userkey
        ];
        if(!is_null($efftype)){
            $data['efftype'] = $efftype;
        }
        if(!is_null($dorderid)){
            $data['dorderid'] = $dorderid;
        }
        if(!is_null($bid)){
            $data['bid'] = $bid;
        }
        return $data;
    }

    /**
     * @return array
     */
    private function buildHeader(){

        return [];
    }

    /**
     * @param $phone
     * @param $timestamp
     * @param $userkey
     * @return string
     */
    private function buildUrl($phone, $timestamp, $userkey){
        $baseurl = $this->config['url'];
        $did = $this->config['did'];
        return "$baseurl?tel=$phone&did=$did&timestamp=$timestamp&userkey=$userkey";
    }

    /**
     * 获取产品列表
     * @param $phone
     * @return mixed
     * @throws \Exception
     */
    public function getProductList($phone)
    {
        $header = $this->buildHeader();
        $timestamp = time();
        $userkey = $this->genUserKey($phone, $timestamp);

        //$url = $this->buildUrl($phone, $timestamp, $userkey);
        //$result = $this->http($url, '', $header, false);

        $data = $this->buildData($phone, $timestamp, $userkey);
        $result = $this->http($this->config['url'].'getdp.do', $data, $header);
        
        return $this->buildResult($result, __FUNCTION__);
    }

    /**
     * 下单接口
     * @param $phone
     * @param $pid
     * @param $dorderId
     * @param int $efftype
     * @return mixed
     * @throws \Exception
     */
    public function order($phone, $pid, $dorderId, $efftype = 2){
        $header = $this->buildHeader();
        $timestamp = time();
        $userkey = $this->genUserKey($phone, $timestamp);

        //$url = $this->buildUrl($phone, $timestamp, $userkey);
        //$result = $this->http($url, '', $header, false);

        $data = $this->buildData($phone, $timestamp, $userkey, $efftype, $dorderId, $pid);
        $result = $this->http($this->config['url'].'sdp.do', $data, $header);

        return $this->buildResult($result, __FUNCTION__);
    }

    /**
     * 订单 状态查询
     * @param $phone
     * @param $pid
     * @param $dorderId
     * @return mixed
     * @throws \Exception
     */
    public function qorder($phone, $pid, $dorderId){
        $header = $this->buildHeader();
        $timestamp = time();
        $userkey = $this->genUserKey($phone, $timestamp);

        //$url = $this->buildUrl($phone, $timestamp, $userkey);
        //$result = $this->http($url, '', $header, false);

        $data = $this->buildData($phone, $timestamp, $userkey, null, $dorderId, $pid);
        $result = $this->http($this->config['url'].'qorder.do', $data, $header);

        return $this->buildResult($result, __FUNCTION__);
    }

    /**
     * 处理 请求结果
     * @param $curlResult
     * @param $method
     * @return mixed
     * @throws \Exception
     */
    public function buildResult($curlResult, $method){
        $result = json_decode($curlResult, true);
        $methodName = $method.'Result';
        if(!empty($result)){
            return $this->$methodName($result);
        }
        throw new \Exception('无法处理的请求结果!', 20001);
    }

    /**
     * 处理 getProductList 的请求结果
     * @param $result
     * @return array
     */
    private function getProductListResult($result){
        $res = [
            'code' => $this->commonMap[$result['status']],
            'msg' => $this->codeMap[$this->commonMap[$result['status']]],
        ];
        $datas = json_decode($result['data'], true);
        $info = [];
        if(!empty($datas)){
            $tmp = [];
            foreach ($datas as $data) {
                $tmp['operator_id'] = $this->operatorMap[$data['spid']];
                $tmp['product'] = $data['bid'];
                $tmp['costprice'] = $data['costprice'];
                $tmp['price'] = $data['price'];
                $tmp['name'] = $data['name'];
                $tmp['size'] = $data['size'];
                $info[] = $tmp;
                $tmp = [];
            }
        }
        $res['info'] = $info;
        return $res;
    }

    /**
     * 处理 order 的请求结果
     * @param $result
     * @return array
     */
    private function orderResult($result){
        $preOrderStatusMap = [
            1 => PRE_ORDER_SUCCESS_STATUS,
            2 => PRE_ORDER_SUCCESS_STATUS,
            -3 => PRE_ORDER_FAILED_STATUS,
        ];
        if(in_array($result['status'], array_keys($preOrderStatusMap))){
            $res = [
                'code' => 1000,
                'msg' => $this->codeMap[1000],
            ];
            $info['status'] = $preOrderStatusMap[$result['status']];
            $res['info'] = $info;
        }else{
            $res = [
                'code' => $this->commonMap[$result['status']],
                'msg' => $this->codeMap[$this->commonMap[$result['status']]],
            ];
        }
        return $res;
    }

    /**
     * 处理 qorder 的请求结果
     * @param $result
     * @return array
     */
    private function qorderResult($result){
        $orderStatusMap = [
            0 => ORDER_NON_EXIST_STATUS,
            1 => ORDER_SUCCESS_STATUS,
            -2 => ORDER_FAILED_STATUS,
            5 => ORDER_IN_PROCESS_STATUS
        ];
        if(in_array($result['status'], array_keys($orderStatusMap))){
            $res = [
                'code' => 1000,
                'msg' => $this->codeMap[1000],
            ];
            $info['status'] = $orderStatusMap[$result['status']];
            $res['info'] = $info;
        }else{
            $res = [
                'code' => $this->commonMap[$result['status']],
                'msg' => $this->codeMap[$this->commonMap[$result['status']]],
            ];
        }
        return $res;
    }
}